package com.app.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.app.util.AppSecurityUtil;

@Controller
public class AppController {

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView getDefault(ModelMap model) {
		if (AppSecurityUtil.isAnonymousUser()) {
			return new ModelAndView("redirect:login");
		} else {
			return new ModelAndView("redirect:success");
		}
	}
	@RequestMapping(value = "/**")
	public ModelAndView handlePageNotFound(HttpServletRequest request,
			ModelMap map) {
		if (request.getRequestURI().equals("/accessDenied")) {
			return new ModelAndView("redirect:login");
		}
		map.addAttribute("URI", request.getRequestURI());
		return new ModelAndView("404");
	}

	@RequestMapping(value = "/accessDenied", method = RequestMethod.GET)
	public ModelAndView accessDenied(HttpServletRequest request, ModelMap map) {
		if (AppSecurityUtil.isAnonymousUser()) {
			return new ModelAndView("redirect:login");
		}
		map.addAttribute("error_msg", "Access Denied");
		return new ModelAndView("success");
	}
}
